package com.lm.jsch.example;

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

public class JSchPortForwarding {
    public static void main(String[] args) {
        String host = "your-server-ip";
        String user = "your-username";
        String password = "your-password";

        try {
            JSch jsch = new JSch();
            Session session = jsch.getSession(user, host, 22);
            session.setPassword(password);
            session.setConfig("StrictHostKeyChecking", "no");
            session.connect();

            // 设置本地端口转发
            int assignedPort = session.setPortForwardingL(8888, "localhost", 8080);
            System.out.println("Local port forwarding: localhost:" + assignedPort + " -> localhost:8080");

            // 保持连接
            Thread.sleep(60000);

            session.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

